﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Outlook;

namespace FogOutlook {
	public partial class ExplorerGroupRibbon {

		private DirectReport dr;

		public ExplorerGroupRibbon() : base(Globals.Factory.GetRibbonFactory()) {
			InitializeComponent();
			dr = new DirectReport(bugTrackerGroup);
		}

		private void FogOutlookExplorerGroup_Load(object sender, RibbonUIEventArgs e) {
			ThisAddIn.app.ActiveExplorer().SelectionChange += new ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
			dr.Visible = Properties.Settings.Default.showDirectToInbox;
			reportFogBugz.Visible = Properties.Settings.Default.showReportOnFogBugz;
			Properties.Settings.Default.Save();
		}

		private void CurrentExplorer_Event() {
			var explorer = ThisAddIn.app.ActiveExplorer();
			try {
				bool bEnable = false;
				foreach(Object sel in explorer.Selection) {
					if(sel is MailItem) {
						bEnable = true;
						break;
					}
				}
				reportBug.Enabled = bEnable;
			}
			catch(System.Exception) { }
		}

		private void newBug_Click(object sender, RibbonControlEventArgs e) {
			if(!CheckConnect())
				return;
			ThisAddIn.BugTracker.OpenBugReport(null, null);
		//	BugReportForm brf = new BugReportForm();
		//	brf.ShowDialog();
		//	brf.Dispose();
		}

		private void reportBug_Click(object sender, RibbonControlEventArgs e) {
			if(!CheckConnect())
				return;

			var explorer = ThisAddIn.app.ActiveExplorer();
			foreach(Object selObject in explorer.Selection) {
				MailItem mi = selObject as MailItem;
				if(mi != null) {
					ThisAddIn.BugTracker.OpenBugReport(mi, ThisAddIn.GetSenderSMTPAddress(mi)); // This sucks :/
			//		BugReportForm brf = new BugReportForm(mi);
				//	brf.ShowDialog();
				}
			}
		}

		private void explorerGroup_DialogLauncherClick(object sender, RibbonControlEventArgs e) {
			using(LoginForm lf = new LoginForm()) {
				if(lf.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
					dr.Visible = Properties.Settings.Default.showDirectToInbox;
					ThisAddIn.TryConnect(lf.username.Text, lf.password.Text, lf.btURL.Text, lf.btType.Text);
					reportFogBugz.Visible = Properties.Settings.Default.showReportOnFogBugz;
					if(ThisAddIn.BugTracker != null)
						ThisAddIn.BugTracker.ShowConfirmation = Properties.Settings.Default.showConfirmation;
					Properties.Settings.Default.Save();
				}
			}
		}

		private bool CheckConnect() {
			if(!ThisAddIn.IsConnected) {
				if(!ThisAddIn.IsConnecting)
					explorerGroup_DialogLauncherClick(null, null);
				return ThisAddIn.WaitConnect();
			}
			return true;
		}

		private void reportFogBugz_Click(object sender, RibbonControlEventArgs e) {
			if(!CheckConnect())
				return;
			String url = Properties.Settings.Default.btURL;

			//htps://zombiewatch.fogbugz.com/default.asp?pg=pgPublicEdit
			var explorer = ThisAddIn.app.ActiveExplorer();
			foreach(Object selObject in explorer.Selection) {
				MailItem mi = selObject as MailItem;
				if(mi != null) {
					//ThisAddIn.BugTracker.DirectReport(project, mi, ThisAddIn.GetSenderSMTPAddress(mi));
					ThisAddIn.BugTracker.ReportOnSite(url, mi, ThisAddIn.GetSenderSMTPAddress(mi));
				}
			}			
/*			String email = "&sCustomerEmail=test%40test.com"
	
			String title = "&sTitle=" + Microsoft.
	
			String content = "&sEvent="
	*/			
				/*A+ZES+%28Zombie+Eradication+Squad%29+will+be+dispatched+as+soon+as+we+get+your+request.+Please+don%27t+forget+to+give+us...%0D%0A%0D%0AApproximate+Number+of+Undead%3A%0D%0A%0D%0ALast+Known+Location%3A*/
		}
/*
		private void directTo_Click(object sender, RibbonControlEventArgs e) {
			if(!CheckConnect())
				return;
//			foreach(base in directTo.Items) {

	//		}
			string project = Properties.Settings.Default.directToProject;
			if(project.Length <= 0) {
				System.Windows.Forms.MessageBox.Show("No project set for reporting directly to FogBugz. Set one from the drop down list by clicking it.", "No project set");
				return;
			}
			var explorer = ThisAddIn.app.ActiveExplorer();
			foreach(Object selObject in explorer.Selection) {
				MailItem mi = selObject as MailItem;
				if(mi != null) {
					ThisAddIn.BugTracker.DirectReport(project, mi, ThisAddIn.GetSenderSMTPAddress(mi));
				}
			}
		}*/
	}
}
